package com.ming.common.beetl.init;

import com.ming.common.beetl.cache.CacheSqlExecUtil;
import com.ming.common.beetl.cache.CacheSqlManagerUtil;
import com.ming.common.beetl.entity.BaseEntity;
import com.ming.common.beetl.entity.DataSourceEntity;
import com.ming.common.beetl.entity.SQLManagerEntity;
import com.ming.common.beetl.enums.DictEnums;
import com.ming.common.beetl.util.SQLManagerUtil;
import com.ming.common.beetl.util.SqlUtil;
import com.ming.common.beetl.util.StrUtil;
import com.zaxxer.hikari.HikariDataSource;
import org.beetl.sql.core.SQLManager;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Component

public class MdaoInit {

    @Resource
    private SQLManager sqlManager;

    @PostConstruct
    public void init(){
        initSQLManagerAll();
        initSQLExecAll();
    }

    public void initSQLExecAll(){
        String tableName = DictEnums.TableInfo.ivy_db_sql_exec.value();
        String primaryKey = DictEnums.TableInfo.ivy_db_sql_exec.label();
        List<? extends BaseEntity> list = SQLManagerUtil.NEW().sqlManager(sqlManager).tableName(tableName).primaryKey(primaryKey).selectAll();
        list.forEach(m->{
            String sqlId = m.getStrValue(StrUtil.snakeToCamel("sql_id"));
            CacheSqlExecUtil.NEW().key(sqlId).put(c -> m);
        });
    }

    public void initSQLManagerAll(){
        String tableName = DictEnums.TableInfo.ivy_db_sql_manager.value();
        String primaryKey = DictEnums.TableInfo.ivy_db_sql_manager.label();

        DictEnums.TableInfo ivy_db_datasource = DictEnums.TableInfo.ivy_db_datasource;
        Map<Long, ? extends BaseEntity> dataSourceMap = SQLManagerUtil.NEW()
                .sqlManager(sqlManager)
                .tableName(ivy_db_datasource.value())
                .primaryKey(ivy_db_datasource.label())
                .selectAllToLongMap(m -> m.getLongValue("id"), Function.identity());

        List<? extends BaseEntity> list = SQLManagerUtil.NEW().sqlManager(sqlManager).tableName(tableName).primaryKey(primaryKey).selectAll();
        list.forEach(m->{
            Long id = m.getLongValue("id");
            String dbStyle = m.getStrValue("dbStyle");
            String smBeanName = m.getStrValue("smBeanName");
            Long datasourceId = m.getLongValue("datasourceId");
            BaseEntity dsEntity = dataSourceMap.get(datasourceId);

            //构建数据源
            DataSourceEntity dataSourceEntity = new DataSourceEntity();
            dataSourceEntity.setDriverClassName(dsEntity.getStrValue("driverClassName"));
            dataSourceEntity.setJdbcUrl(dsEntity.getStrValue("jdbcUrl"));
            dataSourceEntity.setUsername(dsEntity.getStrValue("username"));
            dataSourceEntity.setPassword(dsEntity.getStrValue("password"));
            HikariDataSource hikariDataSource = SqlUtil.buildHikariDataSource(dataSourceEntity);

            //构建beetlsql管理器
            SQLManagerEntity entity = new SQLManagerEntity();
            entity.setDataSource(hikariDataSource);
            entity.setDbStyle(DictEnums.DbStyle.getDbStyle(dbStyle));
            entity.setInitSql(null);
            SQLManager buildSQLManager = SqlUtil.buildSQLManager(entity);

            //缓存beetlsql管理器
            CacheSqlManagerUtil.NEW().key(smBeanName).put(c -> buildSQLManager);
            CacheSqlManagerUtil.NEW().key(id).put(c -> buildSQLManager);
            //CacheSqlManagerUtil2.NEW().key(id).put(c -> buildSQLManager);
            /*Cache<String, SQLManager> cacheList = CacheSqlManagerUtil.getCacheList();
            SQLManager cache = CacheSqlManagerUtil.getCache(smBeanName, SQLManager.class);
            cache.execute(new SQLReady("select * from dev_job"),Map.class);*/
            //System.out.println();
        });
    }

}
